﻿@page "/JavaService"
@using JavaDevToys.Rcl.Models
@using JavaDevToys.Rcl.Service
@using System.Text
<CompareCode @bind-ShowDialog="showCompareCode" NewMothedDict="newMothedDict">

</CompareCode>
<MAlert Dismissible
Type="AlertTypes.Error"
@bind-Value="_alert">
    @errMsg
</MAlert>
<h3>业务类生成</h3>
<MContainer Fluid>
    <MRow Align="@AlignTypes.Center">
        <MCol Cols="6">
            <div class="d-flex pt-6">
                <MSelect Items="settingInfo.JavaProjectInfoList"
                @bind-Value="@selectedJavaProjectName"
                ItemText="u => u.Desc"
                ItemValue="u => u.Name"
                Filled
                Label="选择工程"
                FullWidth="false"
                Dense></MSelect>
                <MButton Color="primary" Class="mt-2  ml-9" OnClick="CreateJavaMode">生成类</MButton>
            </div>
        </MCol>
    </MRow>
    <MRow>
        <MSelect Items="controllerPathList"
        @bind-Value="@selectedControllerPath"
        ItemText="u => u"
        ItemValue="u => u"
        Filled
        Label="Controller输出路径"
        FullWidth="false"
        Dense></MSelect>
    </MRow>
    <MRow>
        <MSelect Items="workPathList"
        @bind-Value="@selectedWorkPath"
        ItemText="u => u"
        ItemValue="u => u"
        Filled
        Label="Work输出路径"
        FullWidth="false"
        Dense></MSelect>
    </MRow>
    <MRow>
        <MSelect Items="workImplPathList"
        @bind-Value="@selectedWorkImplPath"
        ItemText="u => u"
        ItemValue="u => u"
        Filled
        Label="WorkImpl输出路径"
        FullWidth="false"
        Dense></MSelect>
    </MRow>
    <MRow Align="@AlignTypes.Center">
        <MTextarea Clearable
        ClearIcon="mdi-close-circle"
        Label="复制表格内容到此"
        @bind-Value="javaText">
        </MTextarea>
    </MRow>
    <MRow Align="@AlignTypes.Center">
        <MList>
            @if (outFilePathList != null && outFilePathList.Count>0)
            {
                <MButton Color="primary" Class="mt-2  ml-9" OnClick="AddGit">添加到git</MButton>
                @foreach (var item in outFilePathList)
                {
                    <MListItem>
                        <MListItemAction>
                            <MIcon>@item.Value</MIcon>
                        </MListItemAction>

                        <MListItemContent>
                            <MListItemTitle>@item.Key</MListItemTitle>
                        </MListItemContent>
                    </MListItem>
                }
            }
        </MList>
    </MRow>
</MContainer>
@code {
    private bool _alert = false;
    private string errMsg = string.Empty;
    private string _selectedJavaProjectName;
    public string selectedJavaProjectName{ get => _selectedJavaProjectName;set{
            if(value!=_selectedJavaProjectName){
                _selectedJavaProjectName = value;
                OnSelectedProjectChanged(value);
            }
        } }
    private string javaText;
    private SettingInfo settingInfo;
    [Inject]
    public IJavaServiceParser javaServiceParser { get; set; }
    [Inject]
    public IJavaCodeGenerator javaCodeGenerator { get; set; }
    [Inject]
    public IApiInfoParser apiInfoParser { get; set; }
    [Inject]
    public IJavaProjectService javaProjectService { get; set; }
    [Inject]
    public ISettingService settingService { get; set; }
    [Inject]
    public IDirectoryPicker directoryPicker { get; set; }
    private Dictionary<string, string> outFilePathList;

    private List<string> controllerPathList = new List<string>();
    private List<string> workPathList = new List<string>();
    private List<string> workImplPathList = new List<string>();

    private string selectedControllerPath;
    private string selectedWorkPath;
    private string selectedWorkImplPath;

    private Dictionary<string, string> newMothedDict = new Dictionary<string, string>();
    private bool showCompareCode = false;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        settingInfo = settingService.GetSettings(0);
    }

    private void OnSelectedProjectChanged(string projectName)
    {
        JavaProjectInfo javaProjectInfo = settingInfo.JavaProjectInfoList.FirstOrDefault(f => f.Name == projectName);
        if (javaProjectInfo == null)
        {
            errMsg = "请选择工程项目";
            _alert = true;
            return;
        }
        if (string.IsNullOrWhiteSpace(javaProjectInfo.FullPath))
        {
            errMsg = "项目路径未设置，请点击右上角设置按钮进行设置";
            _alert = true;
            return;
        }
        controllerPathList = directoryPicker.SearchAllDirectory(javaProjectInfo.FullPath, "controller");
        workPathList = directoryPicker.SearchAllDirectory(javaProjectInfo.FullPath, "work");
        if (workPathList!=null){
            workImplPathList = workPathList.Select(s => Path.Combine(s, "impl")).ToList();
        }
        selectedControllerPath = controllerPathList?.FirstOrDefault();
        selectedWorkPath=workPathList?.FirstOrDefault();
        selectedWorkImplPath = workImplPathList?.FirstOrDefault();
    }
    private void CreateJavaMode()
    {
        newMothedDict.Clear();
        JavaProjectInfo javaProjectInfo = settingInfo.JavaProjectInfoList.FirstOrDefault(f => f.Name == selectedJavaProjectName);
        javaProjectInfo.ControllerPath = selectedControllerPath;
        javaProjectInfo.WorkPath = selectedWorkPath;
        javaProjectInfo.WorkImplPath = selectedWorkImplPath;
        if (javaProjectInfo == null)
        {
            errMsg = "请选择工程项目";
            _alert = true;
            return;
        }
        if (string.IsNullOrWhiteSpace(javaProjectInfo.ControllerPath))
        {
            errMsg = "缺少controller输出路径";
            _alert = true;
            return;
        }
        if (string.IsNullOrWhiteSpace(javaProjectInfo.WorkPath))
        {
            errMsg = "缺少work输出路径";
            _alert = true;
            return;
        }
        if (string.IsNullOrWhiteSpace(javaProjectInfo.WorkImplPath))
        {
            errMsg = "缺少workImpl输出路径";
            _alert = true;
            return;
        }
        if (string.IsNullOrWhiteSpace(javaText))
        {
            errMsg = "内容不能为空";
            _alert = true;
            return;
        }
        // 解析内容
        var javaServiceInfos = javaServiceParser.Parse(javaText);
        // 生成Java类
        // var pathList = javaCodeGenerator.GenerateService(javaProjectInfo, javaServiceInfos);
        //判断是否已经生成了文件
        var classList = javaServiceInfos.Select(s => s.ClassName).Distinct().ToList();


        //生成controller
        var controllerCodeDict=javaCodeGenerator.GenerateController(javaProjectInfo, javaServiceInfos);
        var workCodeDict=javaCodeGenerator.GenerateWork(javaProjectInfo, javaServiceInfos);
        var workImplCodeDict=javaCodeGenerator.GenerateWorkImpl(javaProjectInfo, javaServiceInfos);
        var controllerMethodCodeDict=javaCodeGenerator.GenerateControllerMethod(javaProjectInfo, javaServiceInfos);
        var workMethodCodeDict = javaCodeGenerator.GenerateWorkMethod(javaProjectInfo, javaServiceInfos);
        var workImplMethodCodeDict = javaCodeGenerator.GenerateWorkImplMethod(javaProjectInfo, javaServiceInfos);

        outFilePathList = new Dictionary<string, string>();
        //判断是否已经生成了文件
        foreach (var item in controllerCodeDict)
        {
            string fileFullPath = Path.Combine(javaProjectInfo.ControllerPath, $"{item.Key}.java");
            if (!File.Exists(fileFullPath)){
                File.WriteAllText(fileFullPath, item.Value, new UTF8Encoding(false));
                outFilePathList.Add(fileFullPath, "mdi-label");
            }else{
                newMothedDict.Add(fileFullPath, controllerMethodCodeDict[item.Key]);
            }
        }

        foreach (var item in workCodeDict)
        {
            string fileFullPath = Path.Combine(javaProjectInfo.WorkPath, $"{item.Key}.java");
            if (!File.Exists(fileFullPath))
            {
                File.WriteAllText(fileFullPath, item.Value, new UTF8Encoding(false));
                outFilePathList.Add(fileFullPath, "mdi-label");
            }else{
                newMothedDict.Add(fileFullPath, workMethodCodeDict[item.Key]);
            }
        }

        foreach (var item in workImplCodeDict)
        {
            string fileFullPath = Path.Combine(javaProjectInfo.WorkImplPath, $"{item.Key}.java");
            if (!File.Exists(fileFullPath))
            {
                File.WriteAllText(fileFullPath, item.Value, new UTF8Encoding(false));
                outFilePathList.Add(fileFullPath, "mdi-label");
            }
            else
            {
                newMothedDict.Add(fileFullPath, workImplMethodCodeDict[item.Key]);
            }
        }
        if (newMothedDict.Count > 0)
            showCompareCode = true;
    }

    private void AddGit()
    {
        foreach (var item in outFilePathList)
        {
            if (javaProjectService.AddGitRepository(item.Key, out string errMsg))
            {
                outFilePathList[item.Key] = "mdi-check";
            }
        }
    }
}
